package com.example.shiwuyouproject.ui.fragment.my.activity

import android.view.KeyEvent
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivitySubjectBinding
import com.example.shiwuyouproject.net.bean.CoomStatus
import com.example.shiwuyouproject.ui.bean.UpDataMessage
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.SubjectLefitAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.SubjectRightAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.DataListsBean
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.SubjectViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.yechaoa.yutilskt.ToastUtil
import org.greenrobot.eventbus.EventBus

/**
 * 学科科目
 */
class SubjectActivity : BaseVmActivity<ActivitySubjectBinding, SubjectViewModel>() {
    override fun viewModelClass(): Class<SubjectViewModel> = SubjectViewModel::class.java

    override fun getViewBinding(): ActivitySubjectBinding {
        return ActivitySubjectBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mBinding.myTitleBar.tvTitle.text = "授课区域"
        mViewModel.getdataLists()
    }

    private var myLefitAdapter: SubjectLefitAdapter? = null
    private var myRightAdapter: SubjectRightAdapter? = null
    override fun initData() {
        myLefitAdapter = SubjectLefitAdapter(null)
        myRightAdapter = SubjectRightAdapter()
        mBinding.myLeftRecyclerView.adapter = myLefitAdapter
        mBinding.myRightRecyclerView.adapter = myRightAdapter

    }

    private var grade = ""
    private var stSubject = ""
    override fun setListener() {
        mBinding.myTitleBar.tvBack.setOnClickListener {
            EventBus.getDefault().post(UpDataMessage(CoomStatus.UPMESSAGE_TYPE))
            finish()
        }
        myLefitAdapter?.setOnItemClickListener { adapter, view, position ->
            myLefitAdapter?.data?.forEach {
                it.subStatus = false
            }
            grade = myLefitAdapter?.data?.get(position)?.key.toString()
            myLefitAdapter?.data?.get(position)?.subStatus = true
            myRightAdapter?.setNewData(myLefitAdapter?.data?.get(position)?.list)
            myLefitAdapter?.notifyDataSetChanged()
        }
        myRightAdapter?.setOnItemClickListener { adapter, view, position ->
            myRightAdapter?.data?.forEach {
                it.subItemStatus = false
            }
            stSubject = myRightAdapter?.data?.get(position)?.key.toString()
            myRightAdapter?.data?.get(position)?.subItemStatus = true
            myRightAdapter?.notifyDataSetChanged()
            mViewModel.jiaoxuetedian(grade, stSubject)
        }
    }

    override fun observe() {
        mViewModel.getPostStatus.observe(this, androidx.lifecycle.Observer<Boolean> {
            if (it) {
                ToastUtil.show("保存成功！")
                setResult(100)
                finish()
            } else {
                ToastUtil.show("保存失败！")
            }

        })
        mViewModel.getdataListsStatus.observe(this, androidx.lifecycle.Observer<DataListsBean> {
            if (it.subject.isNotEmpty()) {
                it.subject[0].subStatus = true
                grade = it.subject[0].key
                myRightAdapter?.setNewData(it.subject[0].list)
            }
            myLefitAdapter?.setNewData(it.subject)
            mViewModel.getRealNameAuthenticationData()
        })

        mViewModel.getGenRenStatus.observe(
            this,
            androidx.lifecycle.Observer<PersonalInformationBean> {
                if (it.grade.isEmpty()) {
                    return@Observer
                }
                if (it.subject.isEmpty()) {
                    return@Observer
                }
                myLefitAdapter?.data?.forEachIndexed { index, subject ->
                    subject.subStatus = it.grade == subject.key
                    if (it.grade == subject.key) {
                        grade = it.grade
                    }
                }
                myRightAdapter?.data?.forEachIndexed { index, subject ->
                    subject.subItemStatus = it.subject == subject.key
                    if (it.subject == subject.key) {
                        stSubject = it.subject
                    }
                }
                myLefitAdapter?.notifyDataSetChanged()
                myRightAdapter?.notifyDataSetChanged()
            })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        EventBus.getDefault().post(UpDataMessage(CoomStatus.UPMESSAGE_TYPE))
        finish()
        return true
    }

}